package com.yanqu.road.server.manger.sdk;

import com.yanqu.road.entity.translations.Translations;
import com.yanqu.road.logic.bussiness.translations.TranslationsBusiness;
import com.yanqu.road.logic.thread.ThreadTaskManger;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.thread.translate.MicrosoftTranslateLanguageTask;
import com.yanqu.road.server.logic.thread.translate.TranslateLanguageTask;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2021/4/15
 * 翻译多语言mgr
 */
public class TranslateLanguageMgr extends TempMgr {

    public static Map<String, Map<String, Translations>> chatLanguageMap = new ConcurrentHashMap<>();

    private static ThreadTaskManger translateThread;

    public static ThreadTaskManger getTranslateThread() {
        return translateThread;
    }

    public static String getContent(String srcContent, String language) {
        if (chatLanguageMap.containsKey(srcContent) && chatLanguageMap.get(srcContent).containsKey(language)) {
            Translations translations = chatLanguageMap.get(srcContent).get(language);
            translations.setUseTimes(translations.getUseTimes() + 1);
            translations.setTime(System.currentTimeMillis());
            return translations.getResultContent();
        }
        return null;
    }

    public static void addTranslateContent(String src, String language, String result) {
        if (!chatLanguageMap.containsKey(src)) {
            chatLanguageMap.put(src, new ConcurrentHashMap<>());
        }
        Translations translations = new Translations();
        translations.setSrcContent(src);
        translations.setResultContent(result);
        translations.setLanguage(language);
        translations.setTime(System.currentTimeMillis());
        translations.setUseTimes(1);
        translations.setInsertOption();
        chatLanguageMap.get(src).put(language, translations);
    }

    @Override
    public boolean reloadData() throws Exception {
        chatLanguageMap = TranslationsBusiness.getChatLanguageMap();
        if (translateThread == null) {
            translateThread = new ThreadTaskManger(1, "TranslateThreadPool");
        }
        return true;
    }

    public static void addTranslateLanguageTask(TranslateLanguageTask translateLanguageTask) {
        translateThread.addTask(1, translateLanguageTask);
    }

    public static void addMicrosoftTranslateLanguageTask(MicrosoftTranslateLanguageTask microsoftTranslateLanguageTask) {
        translateThread.addTask(1, microsoftTranslateLanguageTask);
    }

    public static void addTranslateTask(GeneralTask task) {
        translateThread.addTask(1, task);
    }

    public static void removeBeforeTimeParam(String param) {
        long removeTime = Long.valueOf(param);
        for (Map<String, Translations> translationsMap : chatLanguageMap.values()) {
            for (Translations translations : translationsMap.values()) {
                if (translations.getTime() < removeTime) {
                    chatLanguageMap.get(translations.getSrcContent()).remove(translations.getLanguage());
                    if (chatLanguageMap.get(translations.getSrcContent()).size() == 0) {
                        chatLanguageMap.remove(translations.getSrcContent());
                    }
                }
            }
        }
    }

    public static void removeOverTimeData() {
        for (Map<String, Translations> translationsMap : chatLanguageMap.values()) {
            for (Translations translations : translationsMap.values()) {
                if (System.currentTimeMillis() - translations.getTime() > 10 * DateHelper.DAY_MILLIONS) {
                    chatLanguageMap.get(translations.getSrcContent()).remove(translations.getLanguage());
                    if (chatLanguageMap.get(translations.getSrcContent()).size() == 0) {
                        chatLanguageMap.remove(translations.getSrcContent());
                    }
                }
            }
        }
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (Map<String, Translations> translationsMap : chatLanguageMap.values()) {
            for (Translations translations : translationsMap.values()) {
                if (translations.isInsertOption()) {
                    TranslationsBusiness.add(translations);
                } else if (translations.isUpdateOption()) {
                    TranslationsBusiness.update(translations);
                }
            }
        }
        return true;
    }

    public static String languageCodeTransfer(String language) {
        if (language.equals("zh_tw")) {
            return "zh-TW";
        } else if (language.equals("zh_cn") || language.equals("zh_cm")) {
            return "zh-CN";
        } else if (language.equals("ko_kr")) {
            return "ko";
        } else if (language.equals("ja_jp")) {
            return "ja";
        } else if (language.equals("vi_vn")) {
            return "vi";
        } else if (language.equals("th_th")) {
            return "th";
        } else if (language.equals("en_us")) {
            return "en";
        } else if (language.equals("id_id")) {
            return "in";
        } else if (language.equals("ru_ru")) {
            return "ru";
        }
        return "zh-CN";
    }

    /**
     * 谷歌的语言编码转换成微软的对应语言编码
     * @param language
     * @return
     */
    public static String languageCodeGoogleTransferMicrosoft(String language) {
        if (language.equals("zh-TW")) {
            return "zh-Hant";
        } else if (language.equals("zh-CN")) {
            return "zh-Hans";
        } else if (language.equals("ko")) {
            return "ko";
        } else if (language.equals("ja")) {
            return "ja";
        } else if (language.equals("vi")) {
            return "vi";
        } else if (language.equals("th")) {
            return "th";
        } else if (language.equals("en")) {
            return "en";
        } else if (language.equals("in")) {
            return "id";
        } else if (language.equals("ru")) {
            return "ru";
        }
        return "zh-Hans";
    }
}
